<?php
require_once("conn.php");

function list_all() {
	$q = "SELECT id, name FROM `games_stats`";
	$result = mysql_query( $q );
	$num_rows = mysql_num_rows( $result );
	echo "<table>";
	for ( $i = 0; $i < $num_rows; $i++ ) {
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		echo "
		<tr>
		<td>
		<a href=\"game.php?mode=show&amp;id=" . $row['id'] . "\">" . $row['id'] . "</a>
		</td>
		<td>
		<a href=\"game.php?mode=show&amp;id=" . $row['id'] . "\">" . $row['name'] . "</a>
		</td>
		</tr>
		";
	}
	echo "<table>";
}

function my_games($user) {
	$q = "SELECT id, name, player1, player2 FROM `games_stats` WHERE player1 OR player2 = '" . $user . "'";
	$result = mysql_query( $q );
	$num_rows = mysql_num_rows( $result );
	echo "<table>";
	for ( $i = 0; $i < $num_rows; $i++ ) {
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		echo "
		<tr>
		<td>
		<a href=\"game.php?mode=show&amp;id=" . $row['id'] . "\">" . $row['id'] . "</a>
		</td>
		<td>
		<a href=\"game.php?mode=show&amp;id=" . $row['id'] . "\">" . $row['name'] . "</a>
		</td>
		<td>
		" . $row['player1'] . "VS" . $row['player2'] . "
		</td>
		</tr>
		";
	}
	echo "<table>";
}
	

if( isset ($_GET['mode'] )) {
	switch($_GET['mode']) {
		case('my_games'):
			my_games($_SESSION['user_id']);
		break;
		case('show'):
			show();
		break;
		case('all_games'):
		default:
			list_all();
		break;
	}
} else {
	list_all();
}





echo "
<a href=\"game.php?mode=my_games\">My games</a>
<a href=\"game.php?mode=all_games\">All games</a>
";
?>